// owner-app/pages/my-dwell/index.js
import {
  handlerGobackClick,
  handlerSwitchClick,
  onlyChangeRouter,
  hint,
  checkImg,
  uploadFormdata,
} from "../../../utils/util";
import { config } from "../../../config";
const app = getApp();
import User from "../../../models/user";
const user = new User();

Page({
  /**
   * 页面的初始数据
   */
  data: {
    baseImgUrl: "",
    radioNum: 1,
    tabOne0: [],
    scrollH: "auto",

    //加载中状态
    loading: false,
    //没有更多了
    noMore: false,
    //加载失败
    loadingFailed: false,
    //当前已加载页数
    pageNo: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var query = wx.createSelectorQuery();
    query
      .select("#scrollH")
      .boundingClientRect((res) => {
        let height = res.height;
        this.setData({
          scrollH: height - 73,
        });
      })
      .exec();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var { baseImgUrl } = getApp().globalImgUrl.config;
    this.setData({
      baseImgUrl,
    });

    this.initPageData();
  },
  handlerGobackClick,
  handlerSwitchClick,
  onlyChangeRouter,
  catchTap() {},
  onChangeRadio(e) {
    let tab = this.data.tabOne0.filter((v) => {
      return v.status == 1;
    });

    let item = e.target.dataset.itemchange;
    if (item.status == 1) {
      return;
    }

    // status    1为已选，0为未选
    let data = [
      {
        houseMemberId: tab[0].houseMemberId,
        status: 0,
      },
      {
        houseMemberId: item.houseMemberId,
        status: 1,
      },
    ];

    user.residentialAddressDefault({ param: data }).then((res) => {
      if (res.code == 1000) {
        hint("设为当前居住地址成功");
        this.initPageData();
      }
    });
  },
  anewHandle(e) {
    let data = e.target.dataset.item;
    let params = {
      id: data.houseMemberId,
      passSign: 0,
    };
    uploadFormdata({
      url: "/threefact/baseMember/audit",
      method: "POST",
      data: params,
    })
      .then((res) => {
        if (res.data.code == 1000) {
          hint("提交审核成功");
          setTimeout(() => {
            this.initPageData();
          }, 500);
        }
      })
      .catch((err) => {
        console.log(err);
      });
  },
  deleteHouse(e) {
    let itemData = e.target.dataset.itemdele;
    // if (itemData.kinship != "0") {
    //   hint("您当前不是户主，不能删除房屋信息");
    //   return;
    // }
    user.deleteHouseLive(itemData.houseMemberId).then((res) => {
      if (res.code == 1000) {
        hint("删除成功");
        setTimeout(() => {
          this.initPageData();
        }, 500);
      }
    });
  },
  scrollBottom() {
    if (!this.data.loading && !this.data.noMore) {
      this.setData({
        loading: true,
        pageNo: this.data.pageNo + 1,
      });
      this.getData(
        "residentialAddress",
        "loading",
        "tabOne0",
        "pageNo",
        "loadingFailed",
        "noMore",
        true
      );
    }
  },
  /**
   *
   * @param methodName   同类下面的方法名称
   * @param loadingName  加载状态变量
   * @param dataName     data中设定的数据
   * @param isPage       是否是分页查询
   */
  getData(
    methodName,
    loadingName,
    dataName,
    pageNoName,
    loadingFailedName,
    noMoreName,
    isPage
  ) {
    user[methodName](this.data[pageNoName], 10)
      .then((res) => {
        this.setData({
          [loadingName]: false,
        });

        if (res.result.pages * res.result.size >= res.result.total) {
          this.setData({
            [noMoreName]: true,
          });
          return false;
        }

        //有分页
        if (isPage) {
          this.setData({
            [dataName]: this.data[dataName].concat(res.result.records),
          });
        }
        //没有分页就直接赋值
        else {
          this.setData({
            [dataName]: res.result.records,
          });
        }
      })
      .catch((err) => {
        this.setData({
          [loadingFailedName]: true,
        });
      });
  },
  async initPageData() {
    let tabData = [];
    await user.residentialAddress().then((res) => {
      if (res.code == 1000) {
        tabData = res.result.records;
      }
    });

    if (tabData.length) {
      for (let i = 0; i < tabData.length; i++) {
        for (var j = 0; j < tabData[i].avatar.length; j++) {
          await checkImg(tabData[i].avatar[j])
            .then((res) => {
              tabData[i].avatar[j] = tabData[i].avatar[j];
            })
            .catch((err) => {
              tabData[i].avatar[j] = "";
            });

          this.setData({
            tabOne0: tabData,
          });
        }
      }
    }
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
});
